<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kerabat extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("alumni_model", "am");
        $this->load->model("alumnikantor_model", "km");
    }

    function index() {
        $data["alumni"] = $this->am->getById(@$this->sess->alumniid);
        $data["kantor"] = $this->km->getKantor(@$this->sess->alumniid);
        $data["kerabat"] = $this->am->getKerabat(@$this->sess->alumniid);
        $this->myview($data, "profile/index");
    }
    
    function detail(){
        $data["alumni"] = $this->am->getKerabatById(@$this->uri->segment(3));
        $data["kantor"] = $this->km->getKantor($this->uri->segment(3));
        $this->myview($data, "kerabat/detail");
    }
    
    function edit(){
        if ($this->input->post()) {
            $data["alumni"]->alumninama = $this->input->post("txtNama");
            $data["alumni"]->alumnialamatdomisili = $this->input->post("txtAlamatdomisili");
            $data["alumni"]->alumnikotadomisili = $this->input->post("txtKotadomisili");
            $data["alumni"]->alumnipropinsidomisili = $this->input->post("txtPropinsidomisili");
            $data["alumni"]->alumnicontact = $this->input->post("txtContact");
            $data["alumni"]->alumniemail = $this->input->post("txtEmail");
            $data["alumni"]->alumnijenkel = $this->input->post("ddJenkel");
            $data["alumni"]->alumnistatus = $this->input->post("txtStatus");
            $data["alumni"]->alumnitype = $this->input->post("ddHubungan");

            $data["alumni"]->alumninama == "" ? $data["err"]->alumninama = strErr("mustfilled") : "";
            $data["alumni"]->alumnialamatdomisili == "" ? $data["err"]->alumnialamatdomisili = strErr("mustfilled") : "";
            $data["alumni"]->alumnikotadomisili == "" ? $data["err"]->alumnikotadomisili = strErr("mustchoose") : "";
            $data["alumni"]->alumnipropinsidomisili == "" ? $data["err"]->alumnipropinsidomisili = strErr("mustchoose") : "";
            $data["alumni"]->alumnicontact == "" ? $data["err"]->alumnicontact = strErr("mustfilled") : "";
            $data["alumni"]->alumniemail == "" ? $data["err"]->alumniemail = strErr("mustfilled") : "";
            !validEmail($data["alumni"]->alumniemail) ? $data["err"]->alumniemail = strErr("mustfilled") : "";
            $data["alumni"]->alumnistatus == "" ? $data["err"]->alumnistatus = strErr("mustfilled") : "";

            if (@$data["err"]) {
                $this->myview(@$data, "kerabat/edit");
            } else {
                if ($this->am->update($this->uri->segment(3), $data["alumni"], array("alumniparentid" => $this->sess->alumniid))) {
                    $this->session->set_flashdata("message", "Kerabat berhasil diubah");
                    $this->session->set_flashdata("redir", base_url() . "kerabat/detail/" . $this->uri->segment(3));
                    redirect(base_url() . "alert/success");
                } else {
                    $this->session->set_flashdata("message", "Kerabat gagal diubah");
                    $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
                    redirect(base_url() . "alert/error");
                }
//                if ($this->am->insert($data["alumni"]->alumninama, "-", "", "", "", $data["alumni"]->alumnialamatdomisili, $data["alumni"]->alumnikotadomisili, $data["alumni"]->alumnipropinsidomisili, $data["alumni"]->alumnicontact, $data["alumni"]->alumniemail, "sekretariatpusat", "Y", $data["alumni"]->alumnijenkel, $data["alumni"]->alumnistatus, $data["alumni"]->alumnitype, $this->sess->alumniid, "", "", $this->sess->alumniid, $this->sess->alumniid)) {
//                    $this->session->set_flashdata("message", "Kerabat berhasil disimpan");
//                    $this->session->set_flashdata("redir", base_url() . "profile");
//                    redirect(base_url() . "alert/success");
//                }else{
//                    l_query();
//                    $this->session->set_flashdata("message", "Kerabat gagal didisimpan");
//                    $this->session->set_flashdata("redir", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
//                    redirect(base_url() . "alert/error");
//                }
            }
        } else {
            $data["alumni"] = $this->am->getKerabatById($this->uri->segment(3));
            $this->myview($data, "kerabat/edit");
        }
    }
    
    function work() {
        if ($this->input->post()) {
            $data["alumni"]->kantorid = $this->input->post("txtKantorid");
            $data["alumni"]->bagian = $this->input->post("txtBagian");
            $data["alumni"]->jabatan = $this->input->post("txtJabatan");
            $data["alumni"]->daritahun = $this->input->post("txtDari");
            $data["alumni"]->sampaitahun = $this->input->post("txtSampai");

            if ($data["alumni"]->kantorid == "")
                $data["err"]->kantorid = strErr("mustchoose");
            if ($data["alumni"]->jabatan == "")
                $data["err"]->jabatan = strErr("mustfilled");
            if ($data["alumni"]->bagian == "")
                $data["err"]->bagian = strErr("mustfilled");
            
            $this->am->getById($this->uri->segment(3), array("alumniparentid" => $this->sess->alumniid))?"":$data["err"]->error = "Anda tidak berhak menambah pekerjaan untuk kerabat ini.";

            if (@$data["err"]) {
                $data["alumni"]->kantornama = $this->input->post("txtKantornama");
                $this->myview(@$data, "profile/work");
            } else {
                $id = $this->km->insert($this->uri->segment(3), $data["alumni"]->kantorid, $data["alumni"]->bagian, $data["alumni"]->jabatan, $data["alumni"]->daritahun, $data["alumni"]->sampaitahun);
                $this->session->set_flashdata("message", "Pekerjaan berhasil disimpan");
                $this->session->set_flashdata("redir", base_url() . "kerabat/detail/" . $this->uri->segment(3));
                redirect(base_url() . "alert/success");
            }
        } else {
            $this->myview("", "profile/work");
        }
    }

    function workedit() {
        if ($this->input->post()) {
            $data["alumni"]->kantorid = $this->input->post("txtKantorid");
            $data["alumni"]->bagian = $this->input->post("txtBagian");
            $data["alumni"]->jabatan = $this->input->post("txtJabatan");
            $data["alumni"]->daritahun = $this->input->post("txtDari");
            $data["alumni"]->sampaitahun = $this->input->post("txtSampai");

            if ($data["alumni"]->kantorid == "")
                $data["err"]->kantorid = strErr("mustchoose");
            if ($data["alumni"]->jabatan == "")
                $data["err"]->jabatan = strErr("mustfilled");
            if ($data["alumni"]->bagian == "")
                $data["err"]->bagian = strErr("mustfilled");

            if (@$data["err"]) {
                pre($data["err"]);
                $data["alumni"]->kantornama = $this->input->post("txtKantornama");
                $this->myview(@$data, "profile/work");
            } else {
                $id = $this->km->update($this->uri->segment(4), $data["alumni"], array("alumniid" => $this->uri->segment(3)));
                $this->session->set_flashdata("message", "Pekerjaan berhasil diubah");
                $this->session->set_flashdata("redir", base_url() . "kerabat/detail/" . $this->uri->segment(3));
                redirect(base_url() . "alert/success");
            }
        } else {
            $data["alumni"] = $this->km->getById($this->uri->segment(4));
            $this->myview($data, "profile/work");
        }
    }

}